﻿using System.Collections;
using System.Windows.Forms;

namespace SsmsAddin
{
    ///<summary>
    ///</summary>
    public class ListViewColumnSorter : IComparer
    {
        private int _columnToSort = 0;
        private readonly CaseInsensitiveComparer _objectCompare = new CaseInsensitiveComparer();
        private SortOrder _orderOfSort = SortOrder.None;

        public int Compare(object x, object y)
        {
            var item = (ListViewItem)x;
            var item2 = (ListViewItem)y;
            int num = this._objectCompare.Compare(item.SubItems[this._columnToSort].Text, item2.SubItems[this._columnToSort].Text);
            if (this._orderOfSort == SortOrder.Ascending)
            {
                return num;
            }
            if (this._orderOfSort == SortOrder.Descending)
            {
                return -num;
            }
            return 0;
        }

        ///<summary>
        ///</summary>
        public SortOrder Order
        {
            get
            {
                return this._orderOfSort;
            }
            set
            {
                this._orderOfSort = value;
            }
        }

        ///<summary>
        ///</summary>
        public int SortColumn
        {
            get
            {
                return this._columnToSort;
            }
            set
            {
                this._columnToSort = value;
            }
        }
    }
}
